package edu.pku.sei.act.model.common;

import edu.pku.sei.act.model.mote.AcceptAction;
import edu.pku.sei.act.model.mote.Action;
import edu.pku.sei.act.model.mote.ActivityFinal;
import edu.pku.sei.act.model.mote.Data;
import edu.pku.sei.act.model.mote.DataItem;
import edu.pku.sei.act.model.mote.DecisionNode;
import edu.pku.sei.act.model.mote.InitialNode;
import edu.pku.sei.act.model.mote.LIP;
import edu.pku.sei.act.model.mote.MergeNode;
import edu.pku.sei.act.model.mote.Mote;
import edu.pku.sei.act.model.mote.MoteForPackage;
import edu.pku.sei.act.model.mote.MoteNode;
import edu.pku.sei.act.model.mote.PackageDiagram;
import edu.pku.sei.act.model.mote.PackageI;
import edu.pku.sei.act.model.mote.PackageNode;
import edu.pku.sei.act.model.mote.Radio;
import edu.pku.sei.act.model.mote.SendAction;
import edu.pku.sei.act.model.mote.SimpleDevice;
import edu.pku.sei.act.model.mote.Task;
import edu.pku.sei.act.model.mote.TaskDiagram;
import edu.pku.sei.act.model.mote.TaskEditArea;
import edu.pku.sei.act.model.mote.Timer;
import edu.pku.sei.gmp.model.common.GMPModelFactory;
import edu.pku.sei.gmp.model.shape.GMPNode;
import edu.pku.sei.gmp.model.shape.GMPDiagram;
import edu.pku.sei.gmp.model.shape.GMPShapeElement;
import edu.pku.sei.gmp.model.concept.GMPModelElement;
import edu.pku.sei.gmp.model.common.GMPModel;

public class ArchiModelFactory extends GMPModelFactory {
	private int TaskNum = 1;
	private int diagramNo = 1;
	private int moteNo = 1;
	private int moteForPackageNo = 1;
	
	public GMPModelElement createModelElement(String type, boolean needRegister) {
		GMPModelElement element = null;
		
		if (type.equals(ArchiModelConst.__ARCHIMODEL__)) {
			element = new ArchiModel();
			this.setModel((GMPModel) element);
		}else if(type.equals(ArchiModelConst.__MOTE__)){
			element = new Mote();
			((Mote)element).setName(type + moteNo);
			moteNo++;
		}else if(type.equals(ArchiModelConst.__DATA__)){
			element = new Data();
			//((Data)element).setName("Data");
		}else if(type.equals(ArchiModelConst.__DATAITEM__)){
			element = new DataItem();
		}else if (type.equals(ArchiModelConst.__RADIO__)){
			element = new Radio();
		}else if (type.equals(ArchiModelConst.__TIMER__)){
			element = new Timer();
		}else if (type.equals(ArchiModelConst.__SIMPLEDEVICE__)){
			element = new SimpleDevice();
		}else if (type.equals(ArchiModelConst.__LIP__)){
			element = new LIP();
		}else if (type.equals(ArchiModelConst.__TASK__)){
			element = new Task();
			((Task)element).setName("Task" + TaskNum);
			TaskNum++;
		}else if (type.equals(ArchiModelConst.__PACKAGE__)){
			element = new PackageI();
			((PackageI)element).setName(type);
		}else if (type.equals(ArchiModelConst.__MOTEFORPACKAGE__)){
			element = new MoteForPackage();
			((MoteForPackage)element).setName("Mote" + moteForPackageNo);
			moteForPackageNo++;
		}else if(type.equals(ArchiModelConst.__TASKEDITAREA__)){
			element = new TaskEditArea();
		}else if(type.equals(ArchiModelConst.INITIALNODE)){
			element = new InitialNode();
		}else if(type.equals(ArchiModelConst.ACTION)){
			element = new Action();
		}else if(type.equals(ArchiModelConst.SENDACTION)){
			element = new SendAction();
		}else if(type.equals(ArchiModelConst.ACCEPTACTION)){
			element = new AcceptAction();
		}else if(type.equals(ArchiModelConst.DECISIONNODE)){
			element = new DecisionNode();
		}else if(type.equals(ArchiModelConst.MERGENODE)){
			element = new MergeNode();
		}else if(type.equals(ArchiModelConst.ACTIVITYFINAL)){
			element = new ActivityFinal();
		}
		
		
		if (element == null)
			return null;
		element.setModel(this.getModel());
		
		if (needRegister) {
			element.setId(this.generateId());
			register(element, type);
		}
		return element;	
	}
	
	public GMPShapeElement createShapeElement(String type, boolean needRegister) {
		GMPShapeElement element = null;
		if (type.equals(ArchiModelConst.__ARCHIMODELDIAGRAM__)) {
			element = new GMPDiagram();
			GMPDiagram MoteDiagram = (GMPDiagram) element;
			MoteDiagram.setType(type);
			MoteDiagram.setName(type + diagramNo);
			diagramNo++;
		}if (type.equals(ArchiModelConst.__ARCHIMODELPACKAGEDIAGRAM__)) {
			element = new PackageDiagram();
			PackageDiagram packageDiagram = (PackageDiagram) element;
			packageDiagram.setType(type);
			packageDiagram.setName(type);
		}else if(type.equals(ArchiModelConst.__ARCHIMODELTASKDIAGRAM__)){
			element = new TaskDiagram();
			TaskDiagram taskDiagram = (TaskDiagram)element;
			taskDiagram.setType(type);
			taskDiagram.setName(type);
		}
		else if(type.equals(ArchiModelConst.__MOTE__)) {
			element = new GMPNode();
		}else if(type.equals(ArchiModelConst.__DATA__)){
			element = new GMPNode();
		}else if(type.equals(ArchiModelConst.__RADIO__)){
			element = new GMPNode();
		}else if(type.equals(ArchiModelConst.__TIMER__)){
			element = new GMPNode();
		}else if(type.equals(ArchiModelConst.__SIMPLEDEVICE__)){
			element = new GMPNode();
		}else if(type.equals(ArchiModelConst.__LIP__)){
			element = new GMPNode();
		}else if(type.equals(ArchiModelConst.__TASK__)){
			element = new MoteNode();
		}else if (type.equals(ArchiModelConst.__PACKAGE__)){
			element = new GMPNode();
		}else if (type.equals(ArchiModelConst.__MOTEFORPACKAGE__)){
			element = new PackageNode();
		}else if(type.equals(ArchiModelConst.__TASKEDITAREA__)){
			element = new GMPNode();
		}else if(type.equals(ArchiModelConst.INITIALNODE)){
			element = new GMPNode();
		}else if(type.equals(ArchiModelConst.ACTION)){
			element = new GMPNode();
		}else if(type.equals(ArchiModelConst.SENDACTION)){
			element = new GMPNode();
		}else if(type.equals(ArchiModelConst.ACCEPTACTION)){
			element = new GMPNode();
		}else if(type.equals(ArchiModelConst.DECISIONNODE)){
			element = new GMPNode();
		}else if(type.equals(ArchiModelConst.MERGENODE)){
			element = new GMPNode();
		}else if(type.equals(ArchiModelConst.ACTIVITYFINAL)){
			element = new GMPNode();
		}
		
		if (element == null)
			return null;
		element.setModel(this.getModel());
		if (needRegister) {
			element.setId(this.generateId());
			register(element, type);
		}
		return element;
	}
}
